<!--
 * @Author: lihaogn
 * @Date: 2021-12-05 23:57:21
 * @LastEditTime: 2021-12-11 17:28:12
 * @LastEditor: lihaogn
 * @Description: 修改密码对话框
 * @FilePath: \lix-admin-vue2\src\views\system\user-manage\user-list\modules\EditPasswordDialog.vue
-->
<template>
  <!-- 修改密码对话框 start -->
  <el-dialog
    :close-on-click-modal="false"
    title="修改密码"
    :visible.sync="dialogVisible"
    width="420px"
    top="30vh"
    @close="handleDialogCancel('editPasswordForm')"
  >
    <el-form
      :model="editPasswordForm"
      :rules="editPasswordFormRules"
      ref="editPasswordForm"
      label-width="100px"
    >
      <el-form-item label="原密码" prop="oldPassword" required>
        <el-input
          type="password"
          v-model="editPasswordForm.oldPassword"
          autocomplete="new-password"
          :maxlength="20"
        ></el-input>
      </el-form-item>
      <el-form-item label="新密码" prop="password" required>
        <el-input
          type="password"
          v-model="editPasswordForm.password"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="确认新密码" prop="checkPassword" required>
        <el-input
          type="password"
          v-model="editPasswordForm.checkPassword"
          autocomplete="off"
          v-on:keyup.enter.native="handledialogOk('editPasswordForm')"
        ></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button
        type="primary"
        @click="handledialogOk('editPasswordForm')"
        :loading="okButtonLoading"
      >
        确 定</el-button
      >
    </span>
  </el-dialog>
  <!-- 修改密码对话框 end -->
</template>

<script>
import { reqEditUser } from '../../../../../api/user-manage/user/api'
import { dialogMixin } from '../../../../../mixins/dialog'

export default {
  mixins: [dialogMixin],
  props: {
    editId: {
      type: String,
      required: true
    }
  },
  data() {
    // 定义修改密码校验规则
    let validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'))
      } else {
        // 验证密码规则
        if (value === this.editPasswordForm.oldPassword) {
          callback(new Error('新密码与原密码相同，请重新输入！'))
        }
        if (this.editPasswordForm.checkPassword !== '') {
          // 验证确认密码
          this.$refs.editPasswordForm.validateField('checkPassword')
        }
        callback()
      }
    }
    // 定义修改时确认密码校验规则
    let validateCheckPass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.editPasswordForm.password) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      editPasswordForm: {
        oldPassword: '',
        password: '',
        checkPassword: ''
      },
      editPasswordFormRules: {
        oldPassword: [
          { required: true, message: '请输入原密码', trigger: 'blur' }
        ],
        password: [
          { validator: validatePass, trigger: 'blur' },
          { min: 3, max: 20, message: '长度在3-20个字符', trigger: 'blur' }
        ],
        checkPassword: [
          { validator: validateCheckPass, trigger: 'blur' },
          { min: 3, max: 20, message: '长度在3-20个字符', trigger: 'blur' }
        ]
      }
    }
  },

  methods: {
    /**
     * @description: 修改密码对话框取消事件
     * @param {formName：需要操作的表单}
     * @return {*}
     */
    handleDialogCancel(formName) {
      this.$emit('update:visible', false)
      this.$refs[formName].resetFields()
    },
    /**
     * @description: 修改密码对话框确认事件
     * @param {formName：需要操作的表单}
     * @return {*}
     */
    handledialogOk(formName) {
      this.okButtonLoading = true
      this.$refs[formName].validate(async valid => {
        if (valid) {
          this.editPasswordForm.password = this.$md5(
            this.editPasswordForm.password
          )
          this.editPasswordForm.oldPassword = this.$md5(
            this.editPasswordForm.oldPassword
          )
          const res = await reqEditUser(this.editId, {
            ...this.editPasswordForm,
            type: 'editPassword'
          })
          if (res.success) {
            this.$message.success(res.message)
            this.okButtonLoading = false
            this.dialogVisible = false

            this.$emit('ok')
          } else {
            this.okButtonLoading = false
          }
          this.okButtonLoading = false
        } else {
          this.okButtonLoading = false
          return false
        }
      })
    }
  }
}
</script>

<style></style>
